package com.syni.mdd.sjb.marketing.basemarket.utils.cps;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.marketing.basemarket.entity.dto.CpsBaseResultDto;
import com.syni.mdd.sjb.marketing.basemarket.entity.vo.responseVo.dist.OrderStatisticsDataVo;
import com.syni.mdd.sjb.marketing.basemarket.utils.cps.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@EnableConfigurationProperties(CpsApiConfig.class)
@Slf4j
public class CpsApiUtils {

    @Autowired
    private CpsApiConfig cpsApiConfig;

    public ProductCreateResultDto createPro(ProductCreateDto productCreateDto){
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_pro_create();
        HttpResponse httpResponse = HttpUtil
                .createPost(postUrl)
                .headerMap(productCreateDto.getHeaderMap(),false)
                .body(JSON.toJSONString(productCreateDto))
                .execute();
        log.info("createPro:{}",httpResponse.body());
        ProductCreateResultDto productCreateResultDto = JSON.parseObject(httpResponse.body(),ProductCreateResultDto.class);
        return productCreateResultDto;
    }

    public ProductListResultDto listPro(ProductListDto productListDto){
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_pro_list();
        HttpResponse httpResponse = HttpUtil
                .createGet(postUrl)
                .headerMap(productListDto.getHeaderMap(),false)
                .form(JSON.parseObject(JSON.toJSONString(productListDto)))
                .execute();
        log.info("listPro:{}",httpResponse.body());
        ProductListResultDto productCreateResultDto = JSON.parseObject(httpResponse.body(),ProductListResultDto.class);
        return productCreateResultDto;
    }

    public ProductDetailResultDto deitalPro(ProductDetailDto productDetailDto){
        String postUrl = String.format("%s?productCenterId=%s",cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_pro_detail(),productDetailDto.getProductCenterId());
        HttpResponse httpResponse = HttpUtil
                .createGet(postUrl)
                .headerMap(productDetailDto.getHeaderMap(),false)
                .execute();
        log.info("deitalPro:{}",httpResponse.body());
        ProductDetailResultDto productDetailResultDto = JSON.parseObject(httpResponse.body(),ProductDetailResultDto.class);
        return productDetailResultDto;
    }


    //获取用户订单统计数据
    public OrderStatisticsDataVo userOrder(String unionId,String productId){
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_stat_order();
        Map req = new HashMap();
        req.put("unionId",unionId);
        req.put("productId",productId);
        String result = HttpUtil.createPost(postUrl)
                .header(new HashMap<>())
                .form(req)
                .execute()
                .body();
        JSONObject jo = JSONObject.parseObject(result);
        if("00".equals(jo.getString("status"))){
            return JSONObject.parseObject(jo.getString("data"), OrderStatisticsDataVo.class);
        }else{
            return null;
        }
    }

    //获取商品分享统计数据
    public OrderStatisticsDataVo userShare(String unionId,String productId){
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_stat_share();
        Map req = new HashMap();
        req.put("unionId",unionId);
        req.put("productId",productId);
        String result = HttpUtil.createPost(postUrl)
                .header(new HashMap<>())
                .form(req)
                .execute()
                .body();
        JSONObject jo = JSONObject.parseObject(result);
        if("00".equals(jo.getString("status"))){
            return JSONObject.parseObject(jo.getString("data"), OrderStatisticsDataVo.class);
        }else{
            return null;
        }
    }

    //获取商品分享统计数据
    public OrderStatisticsDataVo userVisits(String unionId,String productId){
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_stat_visits();
        Map req = new HashMap();
        req.put("unionId",unionId);
        req.put("productId",productId);
        String result = HttpUtil.createPost(postUrl)
                .header(new HashMap<>())
                .form(req)
                .execute()
                .body();
        JSONObject jo = JSONObject.parseObject(result);
        if("00".equals(jo.getString("status"))){
            return JSONObject.parseObject(jo.getString("data"), OrderStatisticsDataVo.class);
        }else{
            return null;
        }
    }

    public OrderStatisticsResultDto statisticsUserOrder(OrderStatisticsDto orderStatisticsDto) {
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_stat_order();
        HttpResponse httpResponse = HttpUtil
                .createPost(postUrl)
                .body(JSON.toJSONString(orderStatisticsDto))
                .headerMap(orderStatisticsDto.getHeaderMap(),false)
                .execute();
        log.info("statisticsUserOrder:{}",httpResponse.body());
        OrderStatisticsResultDto orderStatisticsResultDto = JSON.parseObject(httpResponse.body(),OrderStatisticsResultDto.class);
        return orderStatisticsResultDto;
    }

    public OrderStatisticsResultDto statisticsShare(OrderStatisticsDto orderStatisticsDto) {
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_stat_share();
        HttpResponse httpResponse = HttpUtil
                .createGet(postUrl)
                .form(BeanUtils.objectToMap(orderStatisticsDto))
                .headerMap(orderStatisticsDto.getHeaderMap(),false)
                .execute();
        log.info("statisticsShare:{}",httpResponse.body());
        OrderStatisticsResultDto orderStatisticsResultDto = JSON.parseObject(httpResponse.body(),OrderStatisticsResultDto.class);
        return orderStatisticsResultDto;
    }

    public OrderStatisticsResultDto statisticsVisit(OrderStatisticsDto orderStatisticsDto) {
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_stat_share();
        HttpResponse httpResponse = HttpUtil
                .createGet(postUrl)
                .form(BeanUtils.objectToMap(orderStatisticsDto))
                .headerMap(orderStatisticsDto.getHeaderMap(),false)
                .execute();
        log.info("statisticsVisit:{}", httpResponse.body());
        OrderStatisticsResultDto orderStatisticsResultDto = JSON.parseObject(httpResponse.body(), OrderStatisticsResultDto.class);
        return orderStatisticsResultDto;
    }



    /**
     * 资产明细
     */
    public CpsBaseResultDto withdrawal(AssetsWithdrawalDto assetsWithdrawalDto){
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_balance_withdrawal();
        HttpResponse httpResponse = HttpUtil
                .createPost(postUrl)
                .headerMap(assetsWithdrawalDto.getHeaderMap(),false)
                .body(JSON.toJSONString(assetsWithdrawalDto))
                .execute();
        log.info("withdrawal:{}", httpResponse.body());
        CpsBaseResultDto resultDto = JSONObject.parseObject(httpResponse.body(),CpsBaseResultDto.class);
        return resultDto;
    }

    /**
     * 获取余额
     * @return
     */
    public AssetsBalanceResultDto getBalanceByAccountId(AssetsBalanceDto assetsBalanceDto){
        String reqUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_get_balance();
        HttpResponse httpResponse = HttpUtil
                .createGet(reqUrl)
                .headerMap(assetsBalanceDto.getHeaderMap(),false)
                .form(BeanUtils.objectToMap(assetsBalanceDto))
                .execute();
        log.info("getBalanceByAccountId:{}", httpResponse.body());
        AssetsBalanceResultDto resultDto = JSONObject.parseObject(httpResponse.body(),AssetsBalanceResultDto.class);
        return resultDto;
    }

    /**
     * 明细
     */
    public CpsBaseResultDto getBalanceRecord(AssetsBalanceRecordDto assetsBalanceRecordDto){
        String reqUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_balance_record();
        HttpResponse httpResponse = HttpUtil
                .createGet(reqUrl)
                .headerMap(assetsBalanceRecordDto.getHeaderMap(),false)
                .form(BeanUtils.objectToMap(assetsBalanceRecordDto))
                .execute();
        log.info("getBalanceRecord:{}", httpResponse.body());
        CpsBaseResultDto resultDto = JSONObject.parseObject(httpResponse.body(),CpsBaseResultDto.class);
        return resultDto;
    }

    public OrderListResultDto orderList(OrderListDto orderListDto) {
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_order_list();
        HttpResponse httpResponse = HttpUtil
                .createPost(postUrl)
                .body(JSON.toJSONString(orderListDto))
                .execute();
        log.info("orderList:{}",httpResponse.body());
        OrderListResultDto orderListResultDto = JSON.parseObject(httpResponse.body(),OrderListResultDto.class);
        return orderListResultDto;
    }

    public CpsBaseResultDto share(ProductShareDto productShareDto) {
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_pro_share();
        HttpResponse httpResponse = HttpUtil
                .createPost(postUrl)
                .body(JSON.toJSONString(productShareDto))
                .headerMap(productShareDto.getHeaderMap(),false)
                .execute();
        log.info("getShareProduct:{}",httpResponse.body());
        CpsBaseResultDto cpsBaseResultDto = JSON.parseObject(httpResponse.body(),CpsBaseResultDto.class);
        return cpsBaseResultDto;
    }

    public CpsBaseResultDto getShareProduct(ProductShareDto productShareDto) {
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_pro_share();
        HttpResponse httpResponse = HttpUtil
                .createPost(postUrl)
                .body(JSON.toJSONString(productShareDto))
                .headerMap(productShareDto.getHeaderMap(),false)
                .execute();
        log.info("getShareProduct:{}",httpResponse.body());
        CpsBaseResultDto cpsBaseResultDto = JSON.parseObject(httpResponse.body(),CpsBaseResultDto.class);
        return cpsBaseResultDto;
    }

    public ProductShareResultDto getShareProductQrCode(ProductShareDto productShareDto) {
        String postUrl = cpsApiConfig.getCps_domain() + cpsApiConfig.getCps_pro_share_appQrcode();
        HttpResponse httpResponse = HttpUtil
                .createPost(postUrl)
                .body(JSON.toJSONString(productShareDto))
                .headerMap(productShareDto.getHeaderMap(),false)
                .execute();
        log.info("getShareProductQrCode:{}",httpResponse.body());
        ProductShareResultDto productShareResultDto = JSON.parseObject(httpResponse.body(),ProductShareResultDto.class);
        return productShareResultDto;
    }
}
